	import CryptoJS from 'crypto-js';
	//获取阿里云token
	async function getAliyunAccessToken(){
		var that = this;
		var timeStamp = timestampToDate();
		var tokenParam = {
			AccessKeyId:'LTAI5tFNLhLUszjxU5ZhmwJ8',
			Action:'CreateToken',
			Version:'2019-02-28',
			Format:'JSON',
			RegionId:'cn-shanghai',
			Timestamp:timeStamp,
			SignatureMethod:'HMAC-SHA1',
			SignatureVersion:'1.0',
			SignatureNonce:uuid()
			
		}
		//构造规范化的请求字符串
		var querySignString = sortAndConcatUrlParams(tokenParam);
		var stringToSign = 'GET&'+encodeURIComponent('/')+'&'+encodeURIComponent(querySignString);
		var accessKeySecret= '6pIOvefpppXl08B5b0Rd0ZJ7BpwLMZ';
		var signature = encodeURIComponent(hmacSHA1(stringToSign,accessKeySecret+'&'));
		querySignString = signature+'&'+querySignString;
		var url = 'http://nls-meta.cn-shanghai.aliyuncs.com/?Signature='+querySignString;
		var res = await uni.request({
			url:url,
			method:'GET'
		})
		
		return res;
	}
	
	
	
	//获取复刻音色时的阿里云的签名和请求参数
	async function getCosyVoiceAliyunSignature(audioUrl){
		var that = this;
		var timeStamp = timestampToDate();
		var tokenParam = {
			AccessKeyId:'LTAI5tFNLhLUszjxU5ZhmwJ8',
			Action:'CosyVoiceClone',
			Version:'2019-08-19',
			Format:'JSON',
			RegionId:'cn-shanghai',
			Timestamp:timeStamp,
			SignatureMethod:'HMAC-SHA1',
			SignatureVersion:'1.0',
			SignatureNonce:uuid(),
			VoicePrefix:'chaoji',
			Url:audioUrl
			
		}
		//构造规范化的请求字符串
		var querySignString = sortAndConcatUrlParams(tokenParam);
		var stringToSign = 'POST&'+encodeURIComponent('/')+'&'+encodeURIComponent(querySignString);
		var accessKeySecret= '6pIOvefpppXl08B5b0Rd0ZJ7BpwLMZ';
		var signature = encodeURIComponent(hmacSHA1(stringToSign,accessKeySecret+'&'));
		querySignString = signature+'&'+querySignString;
		var url = 'https://nls-slp.cn-shanghai.aliyuncs.com/?Signature='+querySignString;
		tokenParam.Signature = signature;//签名
		var res = {
			signature:signature,
			cosyVoiceParam:tokenParam,
			url:url
		}
		
		return res;
	}
	
	
	//UUID
	function uuid() {
	  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
	    var r = Math.random() * 16 | 0,
	        v = c === 'x' ? r : (r & 0x3 | 0x8);
	    return v.toString(16);
	  });
	}
	
		
	/**
	     * 生成32位随机字符串
	     * @returns {string} 随机字符串
	     */
	    function generateUUID() {
	        return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
	          var r = Math.random() * 16 | 0,
	              v = c === 'x' ? r : (r & 0x3 | 0x8);
	          return v.toString(16);
	        });
	    }
		
	
	function hmacSHA1(message, secret) {
	  return CryptoJS.HmacSHA1(message, secret).toString(CryptoJS.enc.Base64);
	}
	
	//对参数key进行排序后再拼接为url参数字符串
	function sortAndConcatUrlParams(params) {
	  const sortedKeys = Object.keys(params).sort(); // 对参数键进行排序
	  const sortedParams = sortedKeys.map(key => {
	    const encodedKey = encodeURIComponent(key);
	    const encodedValue = encodeURIComponent(params[key]);
	    return `${encodedKey}=${encodedValue}`;
	  });
	  return sortedParams.join('&'); // 使用&拼接参数
	}
	//将时间戳转化为固定格式的日期 
	function timestampToDate() {
	  var now = new Date();
	     // 将当前本地时间转换为UTC时间
		var utc = new Date(now.getTime() - 28800000);
		// 获取UTC时间的时间戳
	  const year = utc.getFullYear(); // 获取年份
	  const month = ('0' + (utc.getMonth() + 1)).slice(-2); // 获取月份，转为两位数
	  const day = ('0' + utc.getDate()).slice(-2); // 获取日，转为两位数
	  const hours = ('0' + utc.getHours()).slice(-2); // 获取小时，转为两位数
	  const minutes = ('0' + utc.getMinutes()).slice(-2); // 获取分钟，转为两位数
	  const seconds = ('0' + utc.getSeconds()).slice(-2); // 获取秒钟，转为两位数
	 
	  // 拼接字符串
	  return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}Z`;
	}
	


	
	
	export default {
		getAliyunAccessToken,
		getCosyVoiceAliyunSignature,
		generateUUID,
		uuid
	}